package com.zf.shuashua.ui.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.LikeVideo;
import com.zf.shuashua.bean.MeLikeVideoList;
import com.zf.shuashua.ui.custom.Divider3ItemDecoration;
import com.zf.shuashua.video.VideoPlayActivity;

import java.util.List;

import io.reactivex.Observable;

/**
 * 喜欢的视频
 */
@SuppressLint("ValidFragment")
public class MineLikeVideoFragment extends BaseListFragment<LikeVideo.DataBean> {
    private RecyclerView rv;
    private MineLikeAdapter mAdapter;
    private String user_id;

    public static MineLikeVideoFragment newInstance(String user_id) {
        Bundle args = new Bundle();
        args.putString("user_id", user_id);
        MineLikeVideoFragment fragment = new MineLikeVideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_minemyideo;
    }

    @Override
    protected void initView() {
        View view = setEmptyLayout(R.layout.empty_like);
        if (getArguments() == null) {
            return;
        }
        user_id = getArguments().getString("user_id");
        if (!TextUtils.isEmpty(user_id)) {
            ((TextView) view.findViewById(R.id.emptyTitle)).setText("该作者还没有喜欢的视频~");
        }
        super.initView();

    }

    @Override
    protected RecyclerView getRecyclerView() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rv.addItemDecoration(new Divider3ItemDecoration(getActivity()));
        return rv;
    }

    @Override
    protected MeiBaseAdapter<LikeVideo.DataBean> getAdapter() {
        mAdapter = new MineLikeAdapter(R.layout.likevideo_item, null);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MeLikeVideoList dataList = new MeLikeVideoList();
                dataList.setDataList(adapter.getData());
                Intent intent = new Intent(getContext(), VideoPlayActivity.class);
                intent.putExtra("position", position);
                intent.putExtra("moreLikeList", dataList);
                startActivity(intent);
            }
        });
        return mAdapter;
    }

    @Override
    protected Observable<Result<List<LikeVideo.DataBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getLikeVideo(user_id, pageNo);
        return getListData(observable);
    }

    private Observable<Result<List<LikeVideo.DataBean>>> getListData(Observable<String> observable) {
        final Result<List<LikeVideo.DataBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    LikeVideo likeVideo = ParseJsonUtils.parseData(s, LikeVideo.class);
                    result.data = likeVideo.getData();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return false;
    }
}